home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / ANSI / c-client / sm_dos.c < prev    next >
C/C++ Source or Header  |  1993-05-11  |  5KB  |  166 lines

  1. /*
  2.  * Program:    Subscription Manager (DOS based)
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    3 December 1992
  13.  * Last Edited:    11 May 1993
  14.  *
  15.  * Copyright 1993 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. #include <stdio.h>
  38. #include <ctype.h>
  39. #include <fcntl.h>
  40. #include "mail.h"
  41. #include "osdep.h"
  42. #include <sys/stat.h>
  43. #include <io.h>
  44. #include "misc.h"
  45. #include "dawz.h"
  46.  
  47. #define SUBSCRIPTIONFILE(t) sprintf (t,"%s\\SUBSCRIB.LST",myhomedir ())
  48.  
  49. /* Subscribe to mailbox
  50.  * Accepts: mailbox name
  51.  * Returns: T on success, NIL on failure
  52.  */
  53.  
  54. long sm_subscribe (char *mailbox)
  55. {
  56.   int fd;
  57.   char *s,*t,*txt,tmp[MAILTMPLEN],tmpx[MAILTMPLEN];
  58.   struct stat sbuf;
  59.   long ret = LONGT;
  60.   SUBSCRIPTIONFILE (tmp);    /* open subscription database */
  61.   if ((fd = open (tmp,O_RDWR|O_CREAT)) >= 0) {
  62.     fstat (fd,&sbuf);        /* get file size and read data */
  63.     read (fd,s = txt = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
  64.     s[sbuf.st_size] = '\0';    /* tie off string */
  65.     while (t = strchr (s,'\n')){/* for each line in database */
  66.       *t++ = '\0';        /* tie off string */
  67.       if (strcmp (s,mailbox)) s = t;
  68.       else {            /* oops, it's subscribed */
  69.     sprintf (tmp,"Already subscribed to mailbox %s",mailbox);
  70.     mm_log (tmp,ERROR);
  71.     ret = NIL;
  72.     break;
  73.       }
  74.     }
  75.     if (ret) {            /* append to end of file if OK */
  76.       lseek (fd,sbuf.st_size,SEEK_SET);
  77.       sprintf (tmp,"%s\n",mailbox);
  78.       write (fd,tmp,strlen (tmp));
  79.     }
  80.     close (fd);            /* close off file */
  81.     fs_give ((void **) &txt);    /* free database */
  82.   }
  83.   else {
  84.     mm_log ("Can't create subscription database",ERROR);
  85.     ret = NIL;
  86.   }
  87.   return ret;            /* all done */
  88. }
  89.  
  90. /* Unsubscribe from mailbox
  91.  * Accepts: mailbox name
  92.  * Returns: T on success, NIL on failure
  93.  */
  94.  
  95. long sm_unsubscribe (char *mailbox)
  96. {
  97.   int fd;
  98.   char *s,*t,*txt,*end,tmp[MAILTMPLEN],tmpx[MAILTMPLEN];
  99.   struct stat sbuf;
  100.   long ret = NIL;
  101.   SUBSCRIPTIONFILE (tmp);    /* open subscription database */
  102.   if ((fd = open (tmp,O_RDWR)) >= 0) {
  103.     fstat (fd,&sbuf);        /* get file size and read data */
  104.     read (fd,s = txt = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
  105.                 /* tie off string */
  106.     *(end = s + sbuf.st_size) = '\0';
  107.     while (t = strchr (s,'\n')){/* for each line in database */
  108.       *t++ = '\0';        /* temporarily tie off string */
  109.       if (strcmp (s,mailbox)) {    /* match? */
  110.     t[-1] = '\n';        /* no, restore newline */
  111.     s = t;            /* try next subscription */
  112.       }
  113.       else {            /* cancel subscription */
  114.     if (t != end) memcpy (s,t,1 + end - t);
  115.     end -= t - s;        /* calculate new end of database */
  116.     ret = T;        /* note cancelled a subscription */
  117.       }
  118.     }
  119.     if (ret) {            /* found any? */
  120.                 /* yes, seek to start of file and write new */
  121.       lseek (fd,(long) 0,SEEK_SET);
  122.       if (end != txt) write (fd,txt,end - txt);
  123.                 /* tie off file */
  124.       chsize (fd,(long) (end - txt));
  125.     }
  126.     else {            /* subscription not found */
  127.       sprintf (tmp,"Not subscribed to mailbox %s",mailbox);
  128.       mm_log (tmp,ERROR);    /* error if at end */
  129.     }
  130.     close (fd);            /* close off file */
  131.     fs_give ((void **) &txt);    /* free database */
  132.   }
  133.   else {
  134.     mm_log ("No subscriptions",ERROR);
  135.     ret = NIL;
  136.   }
  137.   return ret;            /* all done */
  138. }
  139.  
  140. /* Read subscription database
  141.  * Accepts: pointer to subscription database handle (handle NIL if first time)
  142.  * Returns: character string for subscription database or NIL if done
  143.  * Note - uses strtok() mechanism
  144.  */
  145.  
  146. char *sm_read (void **sdb)
  147. {
  148.   int fd;
  149.   char *s,*t,tmp[MAILTMPLEN];
  150.   struct stat sbuf;
  151.   if (!*sdb) {            /* first time through? */
  152.     SUBSCRIPTIONFILE (tmp);    /* open subscription database */
  153.     if ((fd = open (tmp,O_RDONLY)) >= 0) {
  154.       fstat (fd,&sbuf);        /* get file size and read data */
  155.       read (fd,s = (char *) (*sdb = fs_get (sbuf.st_size + 1)),sbuf.st_size);
  156.       close (fd);        /* close file */
  157.       s[sbuf.st_size] = '\0';    /* tie off string */
  158.       if (t = strtok (s,"\n")) return t;
  159.     }
  160.   }
  161.                 /* subsequent times through database */
  162.   else if (t = strtok (NIL,"\n")) return t;
  163.   fs_give (sdb);        /* free database */
  164.   return NIL;            /* all done */
  165. }
  166.